package xyz.bali16.module.netdisk.controller;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import xyz.bali16.application.core.utils.AuthUtils;
import xyz.bali16.application.core.utils.TimeUtil;
import xyz.bali16.application.entity.User;
import xyz.bali16.module.netdisk.entity.FileFolder;
import xyz.bali16.module.netdisk.entity.FileStorage;
import xyz.bali16.module.netdisk.entity.FileUser;
import xyz.bali16.module.netdisk.enums.FilePermissionEnum;
import xyz.bali16.module.netdisk.service.FileFolderService;
import xyz.bali16.module.netdisk.service.FileStorageService;
import xyz.bali16.module.netdisk.service.FileUserService;
import xyz.bali16.module.netdisk.service.UploadFileService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author bali2 on 7/4/2023.
 * @version 1.0
 */
public class NetdiskBaseController extends BaseController {

    @Autowired
    protected UploadFileService uploadFileService;
    @Autowired
    protected FileFolderService fileFolderService;
    @Autowired
    protected FileStorageService fileStorageService;
    @Autowired
    protected FileUserService fileUserService;

    protected FileUser fileUser;

    @Override
    @ModelAttribute
    //todo 这里应该弄事务 一起提交两个
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {
        super.setReqAndRes(request, response);

        User sysUser = userService.getByUsername(AuthUtils.getCurrentUsername());
        if (sysUser != null) {
            String userId = sysUser.getUserId();
            fileUser = fileUserService.getFileUserBySysUserId(userId);
            System.out.println(fileUser);
            /**新网盘用户创建*/
            if (fileUser == null) {
                System.out.println("新用户创建");
                System.out.println(sysUser);
                fileUser = FileUser.builder().userId(userId).build();
                // 不存在网盘用户则创建网盘用户
                if (fileUserService.save(fileUser)) { //重新获取新注册的文件用户
                    fileUser = fileUserService.getFileUserBySysUserId(userId);
                    System.out.println("创建文件用户成功");
                }
                // 默认创建1GB配额，可上传可下载,当前使用空间为0KB。
                FileStorage fileStorage = FileStorage.builder().fileUserId(fileUser.getId()).permission(FilePermissionEnum.UPLOAD_DOWNLOAD.getStatus()).maxSize(1024 * 1024).currentSize(0).build();
                if (fileStorageService.save(fileStorage)) {
                    // 寻找可用的文件仓库并且设置用户的默认仓库
                    fileStorage = fileStorageService.getAvailableStoragesByFileUserId(fileUser.getId()).get(0);
                    fileUser.setFileStorageId(fileStorage.getId());
                    fileUserService.saveOrUpdate(fileUser);
                    System.out.println("创建用户文件仓库成功");
                }
                // 创建用户专有根目录
                FileFolder fileFolder = new FileFolder();
                fileFolder.setFileStorageId(fileUser.getFileStorageId());
                fileFolder.setName(AuthUtils.getCurrentUsername());
                fileFolder.setParentFolderId(fileUser.getFileStorageId());
                fileFolder.setGmtCreated(TimeUtil.getNowTimeToSeconds());
                fileFolderService.save(fileFolder);
            }
            // 如果用户没有指定的文件仓库
            if (fileUser.getFileStorageId() == null) {
                // 设置默认的文件仓库
                List<FileStorage> fileStorage = fileStorageService.getAvailableStoragesByFileUserId(fileUser.getId());
                if (CollectionUtil.isNotEmpty(fileStorage)) {
                    /**设置用户默认仓库id*/
                    /**0为公共仓库*/
                    fileUser.setFileStorageId(fileStorage.get(0).getId());
                    fileUserService.saveOrUpdate(fileUser);
                }
            }
        }
    }
}

